<template>
  <div class="main">
    <a-form
      id="formLogin"
      class="user-layout-login"
      ref="formLogin"
      :form="form"
      @submit="handleSubmit"
    >
      <a-tabs
        :activeKey="customActiveKey"
        :tabBarStyle="{ textAlign: 'center', borderBottom: 'unset' }"
      >
        <a-tab-pane key="tab1" :tab="__('member.login.tab-login-credentials')">
          <a-form-item>
            <a-input
              size="large"
              type="text"
              :placeholder="__('member.login.account.placeholder')"
              v-decorator="[
                'account',
                {rules: [{ required: true, message: __('member.account.required') }], validateTrigger: 'change'}
              ]"
            >
              <a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }"/>
            </a-input>
          </a-form-item>

          <a-form-item>
            <a-input-password
              size="large"
              :placeholder="__('member.login.password.placeholder')"
              v-decorator="[
                'password',
                {rules: [{ required: true, message: __('member.password.required') }], validateTrigger: 'blur'}
              ]"
            >
              <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }"/>
            </a-input-password>
          </a-form-item>
        </a-tab-pane>
      </a-tabs>

      <a-form-item style="margin-top:24px">
        <a-button
          size="large"
          type="primary"
          htmlType="submit"
          class="login-button"
          :loading="state.loginBtn"
          :disabled="state.loginBtn"
        >{{ __('member.login.login') }}</a-button>
      </a-form-item>

      <a-form-item>
        <router-link
          :to="{ name: 'recover', params: { user: 'aaa'} }"
          class="forge-password"
          style="float: right;"
        >{{ __('member.login.forgot-password') }}</router-link>
        <router-link class="register" :to="{ name: 'register' }">{{ __('member.login.signup') }}</router-link>
      </a-form-item>
    </a-form>

    <pic-captcha
      v-if="requiredPicCaptcha"
      :visible="picCaptchaVisible"
      @success="picCaptchaSuccess"
      @cancel="picCaptchaCancel"
    ></pic-captcha>
  </div>
</template>

<script>
import PicCaptcha from '@/components/tools/PicCaptcha'
import { mapActions } from 'vuex'
import { timeFix } from '@/utils/util'
import api from '@/api'

export default {
  components: {
    PicCaptcha
  },
  data () {
    return {
      customActiveKey: 'tab1',
      loginBtn: false,
      isLoginError: false,
      requiredPicCaptcha: true,
      picCaptchaVisible: false,
      form: this.$form.createForm(this),
      state: {
        time: 60,
        loginBtn: false,
        smsSendBtn: false
      }
    }
  },
  created () {
    api.auth.getPicCaptchaConfig({ }).then(response => {
      this.requiredPicCaptcha = response.data.data.picCaptcha
    }).catch(() => {
        this.requiredPicCaptcha = false
      })
  },
  methods: {
    ...mapActions(['Login', 'Logout']),
    handleSubmit (e) {
      e.preventDefault()
      const {
        form: { validateFields },
        state,
        customActiveKey
      } = this

      state.loginBtn = true

      const validateFieldsKey = customActiveKey === 'tab1' ? ['account', 'password'] : ['mobile', 'captcha']

      validateFields(validateFieldsKey, { force: true }, (err, values) => {
        if (!err) {
          if (this.requiredPicCaptcha) {
            this.picCaptchaVisible = true
          } else {
            this.loginHandle(values)
          }
        } else {
          setTimeout(() => {
            state.loginBtn = false
          }, 600)
        }
      })
    },
    picCaptchaSuccess (captchaData) {
      const {
        form
      } = this
      var data = form.getFieldsValue()
      data.code = captchaData.values.code
      this.loginHandle(data).finally(() => {
        captchaData.vm.stepLoading = false
      })
    },
    picCaptchaCancel () {
      this.loginBtn = false
      this.picCaptchaVisible = false
      this.state.loginBtn = false
    },
    loginHandle (values) {
      const {
        Login,
        state
      } = this
      return Login(values)
        .then((res) => this.loginSuccess(res))
        .catch(err => this.requestFailed(err))
        .finally(() => {
          state.loginBtn = false
        })
    },
    loginSuccess (res) {
      this.$router.push({ path: '/' })
      // 延迟 1 秒显示欢迎信息
      setTimeout(() => {
        this.$notification.success({
          message: '欢迎',
          description: `${timeFix()}，欢迎回来`
        })
      }, 1000)
      this.isLoginError = false
    },
    requestFailed () {
      this.isLoginError = true
    }
  }
}
</script>

<style lang="less" scoped>
.user-layout-login {
  label {
    font-size: 14px;
  }

  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .forge-password {
    font-size: 14px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .item-icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: #1890ff;
      }
    }

    .register {
      float: right;
    }
  }
}
</style>
